var adBaiDu = require("../../ads/baiDu/index");
var clickUtils = require("../../utils/clickUtils");
var randomUtils = require("../../utils/randomUtils");
var textUtils = require("../../utils/textUtils");
var ocrUtils = require("../../utils/ocrUtils");
var pointMap = require("./pointMap");
var TypeEnum = {
  直接领取: "zhijielingqu",
  领特权: "lingtequan",
};
module.exports = {
  init(type) {
    if (this.overCheck()) {
      return;
    }
    if (this.adCheck()) {
      return;
    }
    if (this.keLingCheck()) {
      return;
    }
    if (type != TypeEnum.直接领取 && this.signCheck()) {
      return;
    }
    if (this.wxCheck()) {
      return;
    }
    if (type != TypeEnum.领特权 && this.lingTeQuan()) {
      return;
    }
  },
  lingTeQuan() {
    var content = "看广告立即领特权";
    var TextUI = textUtils.existsOneTextStartsWith(content);
    if (TextUI) {
      clickUtils.clickPointMap(pointMap.任务.领特权.关闭);
      sleep(randomUtils.getRandom(2, 3, 1000));
      this.init(TypeEnum.领特权);
      return true;
    }
    return false;
  },
  wxCheck() {
    var content = "可以提现";
    var TextUI = textUtils.existsTextStartsWith(content);
    if (TextUI) {
      clickUtils.clickPointMap(pointMap.任务.可以提现啦.关闭);
      sleep(randomUtils.getRandom(2, 3, 1000));
      return true;
    }
    var TextOCR = ocrUtils.captureExistsText(content);
    if (TextOCR) {
      clickUtils.clickPointMap(pointMap.任务.可以提现啦.关闭);
      sleep(randomUtils.getRandom(2, 3, 1000));
      return true;
    }
    return false;
  },
  signCheck() {
    var contents = ["直接领取", "我知道了"];
    var TextUI = textUtils.existsTextStartsWith(contents);
    if (TextUI) {
      clickUtils.clickTextUI(TextUI);
      sleep(randomUtils.getRandom(2, 3, 1000));
      this.init(TypeEnum.直接领取);
      return true;
    }
    var TextOCR = ocrUtils.captureExistsOneText(contents);
    if (TextOCR) {
      clickUtils.clickTextOCR(TextOCR);
      sleep(randomUtils.getRandom(2, 3, 1000));
      this.init(TypeEnum.直接领取);
      return true;
    }
    return false;
  },
  overCheck() {
    var contents = [
      //prettier-ignore
      "开心收下",
      "立即收下",
    ];
    var TextUI = textUtils.existsOneTextStartsWith(contents);
    if (TextUI) {
      clickUtils.clickTextUI(TextUI);
      sleep(randomUtils.getRandom(1.5, 2, 1000));
      return true;
    }
    var contents = [
      //prettier-ignore
      "开心收下",
      "立即收下",
    ];
    var TextOCR = ocrUtils.captureExistsOneText(contents);
    if (TextOCR) {
      clickUtils.clickTextOCR(TextOCR);
      sleep(randomUtils.getRandom(1.5, 2, 1000));
      return true;
    }
  },
  keLingCheck() {
    var contents = ["可领"];
    var TextOCR = ocrUtils.captureExistsOneText(contents);
    if (TextOCR) {
      clickUtils.clickTextOCR(TextOCR);
      sleep(randomUtils.getRandom(1.5, 2, 1000));
      return true;
    }
  },
  adCheck() {
    var contents = [
      //prettier-ignore
      "看广告再得",
      "看广告得",
      "看广告领",
      "看视频再得",
      "看视频得",
      "看视频领",
      "观看视频再得",
      "观看视频得",
      "观看视频领",
    ];
    var TextUI = textUtils.existsOneTextStartsWith(contents);
    if (TextUI) {
      clickUtils.clickTextUI(TextUI);
      sleep(randomUtils.getRandom(3.5, 5, 1000));
      var maxAdCount = randomUtils.getRandom(1, 4);
      adBaiDu.startWatchAd(maxAdCount);
      this.init();
      return true;
    } else {
      return false;
    }
  },
};
